//
//  HMStackViewBar.swift
//  swift微博项目
//
//  Created by huodian on 16/10/8.
//  Copyright © 2016年 huodian. All rights reserved.
//

import UIKit

enum EmojiType:Int {
    case RECENT = 0
    case DEFAUALT
    case EMOJI
    case LANGXH
}

class HMStackViewBar: UIStackView {

  
    var lastBtn:UIButton? = UIButton()
    
    
    var emojiBeselectedWithTheViewScroll:((EmojiType) -> ())?
    
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        axis = .horizontal
        
        distribution = .fillEqually
        
        setMYUI()
        
        //使用uistackview时要注意其tag的默认值是0,在使用或者设置枚举等需要默认值的时候记得要改变其UIstackView的默认值(如果不设置这导致collectionView无法滑动)
        self.tag = 10000
        
        
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setMYUI(){
       
     
        creatBottomBtn(title: "最近", imageNamed: "compose_emotion_table_left",index: .RECENT)

        creatBottomBtn(title: "第二个", imageNamed: "compose_emotion_table_mid",index: .DEFAUALT)
        
        creatBottomBtn(title: "第三个", imageNamed: "compose_emotion_table_mid",index: .EMOJI)
        
        creatBottomBtn(title: "浪小花", imageNamed: "compose_emotion_table_right",index: .LANGXH)
        
     
        
    }
    
    func creatBottomBtn(title:String,imageNamed:String,index:EmojiType){
      
        let btn  = UIButton()
        
        btn.tag = index.rawValue
        
        btn.addTarget(self, action: #selector(emojiScrollerWithTheViewScroll(btn:)), for: UIControlEvents.touchUpInside)
        
        btn.setTitle(title, for: UIControlState.normal)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        btn.setBackgroundImage(UIImage(named: imageNamed + "_normal"), for: UIControlState.normal)
        
        btn.setBackgroundImage(UIImage(named: imageNamed + "_selected"), for: UIControlState.selected)
        
        btn.setTitleColor(UIColor.white, for: UIControlState.normal)
        
        btn.setTitleColor(UIColor.darkGray, for: .selected)
        
        self.addArrangedSubview(btn)
        //设置页面出现时底部选中状态
        if index == .RECENT {
            
            lastBtn = btn
            
            btn.isSelected = true
            
        }
       
    }
    
    func emojiScrollerWithTheViewScroll(btn:UIButton){
        
        lastBtn?.isSelected = false
        
        lastBtn = btn
        
        btn.isSelected = true
        
        emojiBeselectedWithTheViewScroll!(EmojiType.init(rawValue: btn.tag)!)
       
    }
    
    func selectedEmojiView(indexpath:IndexPath){
        
        let selectedBtn = viewWithTag(indexpath.section) as! UIButton
        
        
        if selectedBtn.isSelected {
            
            return
            
        }
        
        
        lastBtn?.isSelected = false
        
        lastBtn = selectedBtn
        
        selectedBtn.isSelected = true
        
        
        
        
        
    }
}
